from utils.file_operation import load_json_to_dict, save_dict_to_json
import requests
import json
import os


abs_file_path = os.path.abspath(__file__)
# user_name = "13250511220"
# user_password = "A123456789a"


def get_login() -> None:
    """登录账号"""
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "content-type": "application/json; charset=UTF-8",
        "origin": "https://mall.labgic-ljk.com",
        "referer": "https://mall.labgic-ljk.com/login",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
    }

    url = "https://mall.labgic-ljk.com/api/mall4cloud_auth/ua/login"

    # 登录参数
    data = {"principal": "13250511220", "credentials": "A123456789a", "sysType": 0}
    data = json.dumps(data, separators=(",", ":"))
    response = requests.post(url, headers=headers, data=data)

    if response.status_code == 200 and response.json().get("success") == True:
        save_dict_to_json(response.json(), os.path.join(get_file_path(), "login.json"))
    else:
        return None


def search_product(text: str) -> dict:
    """2.根据货号搜索商品"""
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "authorization": "Rl4Qwvs234ZB94ONTwbRjawNMiOnChufu4ljrfHjFlRg1ICPi4BSGdH+joP0sxUz",
        "content-type": "application/json; charset=UTF-8",
        "origin": "https://www.labgic.online",
        "priority": "u=1, i",
        "sec-ch-ua": '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": '"macOS"',
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "cross-site",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
    }
    url = "https://mall.labgic-ljk.com/api/mall4cloud_product/ma/spu/ua/search"
    params = {"pageNum": "1", "pageSize": "15"}
    data = {
        "isSales": 0,
        "orderField": 1,
        "orderType": 2,
        "brandIdFirstList": [9],
        "officeId": 33,
        "text": text,
        "companyId": 21265,
    }
    data = json.dumps(data, separators=(",", ":"))
    response = requests.post(url, headers=headers, params=params, data=data)

    return response.json()


def get_inventory(erpCode: str) -> dict:
    """3.获取库存

    Args:
        erpCode (str): 商品编码

    Returns:
        dict: 库存信息
    """
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "authorization": "Rl4Qwvs234ZB94ONTwbRjawNMiOnChufu4ljrfHjFlRg1ICPi4BSGdH+joP0sxUz",
        "content-type": "application/json; charset=UTF-8",
        "origin": "https://www.labgic.online",
    }
    url = "https://mall.labgic-ljk.com/api/mall4cloud_product/inventory/countErpCodeByWarehouseCode"
    params = {"companyId": "21265", "erpCode": erpCode, "warehouseCode": "HNGZ"}
    response = requests.get(url, headers=headers, params=params)
    return response.json()


def get_file_path():
    return os.path.dirname(abs_file_path)
